/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.supernote.ui

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.example.supernote.data.AppContainer
import com.example.supernote.ui.manager.ManagerRoute
import com.example.supernote.ui.manager.ManagerViewModel
import com.example.supernote.ui.note.NoteRoute
import com.example.supernote.ui.note.NoteViewModel
import com.example.supernote.ui.settings.SettingsRoute
import com.example.supernote.ui.settings.SettingsViewModel
import com.example.supernote.ui.trash.TrashRoute
import com.example.supernote.ui.trash.TrashViewModel

@Composable
fun SuperNoteNavGraph(
    willCheckoutManager: Boolean,
    setWillCheckoutManager: (Boolean) -> Unit,
    appContainer: AppContainer,
    isExpandedScreen: Boolean,
    modifier: Modifier = Modifier,
    navigationActions: SuperNoteNavigationActions = SuperNoteNavigationActions(rememberNavController()),
    openDrawer: () -> Unit = {},
    startDestination: String = SuperNoteDestinations.MANAGER_ROUTE,
) {
    NavHost(
        navController = navigationActions.navController,
        startDestination = startDestination,
        modifier = modifier
    ) {
        composable(SuperNoteDestinations.MANAGER_ROUTE) {
            val managerViewModel: ManagerViewModel = viewModel(
                factory = ManagerViewModel.provideFactory(
                    appContainer.fileEntityRepository
                )
            )
            val noteViewModel: NoteViewModel = viewModel(
                factory = NoteViewModel.provideFactory(appContainer.fileEntityRepository)
            )
            if (willCheckoutManager)
                ManagerRoute(
                    noteViewModel = noteViewModel,
                    managerViewModel = managerViewModel,
                    navigateToNote = {
                        setWillCheckoutManager(false)
                    },
                    isExpandedScreen = isExpandedScreen,
                    openDrawer = openDrawer
                )
            else
                NoteRoute(
                    onBack = {
                        setWillCheckoutManager(true)
                    },
                    noteViewModel = noteViewModel
                )
        }
        composable(SuperNoteDestinations.TRASH_ROUTE) {
            val trashViewModel: TrashViewModel = viewModel(
                factory = TrashViewModel.provideFactory(
                    appContainer.fileEntityRepository
                )
            )
            TrashRoute(
                trashViewModel = trashViewModel,
                isExpandedScreen = isExpandedScreen,
                openDrawer = openDrawer
            )
        }
        composable(SuperNoteDestinations.SETTINGS_ROUTE) {
            val settingsViewModel: SettingsViewModel = viewModel(
                factory = SettingsViewModel.provideFactory(
                )
            )
            SettingsRoute(
                settingsViewModel = settingsViewModel,
                isExpandedScreen = isExpandedScreen,
                openDrawer = openDrawer
            )
        }
    }
}
